package com.kaiwukj.android.ufamily.mvp.ui.page.service.repairs;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.PostRepairParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.RepairsResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.kaiwukj.android.ufamily.utils.upload.OnUploadListener;
import com.kaiwukj.android.ufamily.utils.upload.UploadBean;
import com.kaiwukj.android.ufamily.utils.upload.UploadUtils;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */

public class RepairsPresenter extends BasePresenter<ServiceContract.Model, ServiceContract.RepairsView> {

    public static final int COMMIT = 1;
    public static final int CONFIRM_REPAIR_FINISH = 2;//确认完成
    public static final int EVALUATE = 10;//评价

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public RepairsPresenter(ServiceContract.Model model, ServiceContract.RepairsView rootView) {
        super(model, rootView);
    }

    /**
     * 获取七牛云Token
     */
    public void requestQIToken() {
        mModel.getQiNiuToken()
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<String>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        mRootView.onGetQiNiuToken(result);
                    }
                });
    }

    /**
     * 提交报修
     */
    public void postRepair(PostRepairParams request) {
        mModel.postRepair(request)
                .doOnSubscribe(act -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, COMMIT) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("提交成功");
                    }
                });
    }

    /**
     * 获取报修列表
     */
    public void requestRepairsList(int page, int pageSize) {
        mModel.fetchRepairList(page, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<RepairsResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<RepairsResult> result) {
                        mRootView.onFetchRepairsList(result.getList());
                    }
                });
    }

    /**
     * 报修完成
     */
    public void updateStatus(int repairId) {
        mModel.confirmRepairFinish(repairId)
                .doOnSubscribe(act -> mRootView.showLoading("正在处理..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, CONFIRM_REPAIR_FINISH) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("处理成功");
                    }
                });
    }

    private void postEvaluate(int id, String content, String imgList) {
        mModel.postRepairEvaluate(id, content, imgList)
                .doOnSubscribe(act -> mRootView.showLoading("正在提交..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, COMMIT) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                    }
                });
    }

    public void evaluate(int id, String content, List<String> imgList) {
        if (imgList == null || imgList.size() == 0) {
            postEvaluate(id, content, null);
        } else {
            mModel.getQiNiuToken()
                    .doOnSubscribe(act -> mRootView.showLoading("正在处理..."))
                    .doFinally(() -> mRootView.hideLoading())
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .compose(RxUtils.schedulerHelper())
                    .subscribe(new ApiCallBack<String>(mErrorHandler, mRootView, EVALUATE) {
                        @Override
                        public void onSuccess(@NonNull String result) {
                            UploadUtils.getInstance().uploadImages(imgList, result, new OnUploadListener() {
                                @Override
                                public void onUploadImage(UploadBean result) {

                                }

                                @Override
                                public void onUploadImages(List<UploadBean> results) {
                                    StringBuilder sb = new StringBuilder();
                                    for (int i = 0; i < results.size(); i++) {
                                        if (i != 0) {
                                            sb.append(",");
                                        }
                                        sb.append(results.get(i).getNormalUrl());
                                    }
                                    postEvaluate(id, content, sb.toString());
                                }

                                @Override
                                public void onError(int errCode, String errMsg) {

                                }
                            });
                        }
                    });
        }
    }


}
